package com.xiyoukt.springboot.controller;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.xiyoukt.springboot.Result;
import com.xiyoukt.springboot.entity.User;
import com.xiyoukt.springboot.service.UserService;
import com.xiyoukt.springboot.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    private Logger log = LoggerFactory.getLogger(UserController.class);

    @GetMapping("test")
    public void test(){
        log.trace("log info {}", 1);
        log.debug("log info");
        log.info("log info");
        log.warn("log info");
        log.error("log info");

        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        ch.qos.logback.classic.Logger logger = loggerContext.getLogger(UserController.class);
        logger.setLevel(Level.toLevel("INFO"));
    }

    // post
    @GetMapping("addUser")
    public Result<User> addUser(User user){
        ResultUtil<User> resultUtil = new ResultUtil<>();
        userService.addUser(user);
        return resultUtil.success(user);
    }

    @GetMapping("getAll")
    public Result<List<User>> getAll(){
        ResultUtil<List<User>> resultUtil = new ResultUtil<>();
        List<User> all = userService.getAll();
        return resultUtil.success(all);
    }

    @GetMapping("delUser")
    public Result<Boolean> delUser(Long id){
        ResultUtil<Boolean> resultUtil = new ResultUtil<>();
        userService.delUser(id);
        return resultUtil.success(Boolean.TRUE);
    }

    @GetMapping("pageUser")
    public Result<List<User>> pageUser(){
        ResultUtil<List<User>> resultUtil = new ResultUtil<>();
        List<User> all = userService.pageUser(0, 2);
        return resultUtil.success(all);
    }

    @GetMapping("addTwoUser")
    public Result<User> addTwoUser(User user){
        ResultUtil<User> resultUtil = new ResultUtil<>();
        userService.addTwoUser(user);
        return resultUtil.success(user);
    }

    @GetMapping("addTwoDoubleDataSourceUser")
    public Result<User> addTwoDoubleDataSourceUser(User user){
        ResultUtil<User> resultUtil = new ResultUtil<>();
        userService.addTwoDoubleDataSourceUser(user);
        return resultUtil.success(user);
    }

    @GetMapping("getUser")
    public Result<User> getUser(Long id){
        ResultUtil<User> resultUtil = new ResultUtil<>();
        User user = userService.getUser(id);
        return resultUtil.success(user);
    }



}
